//
//  MainController.m
//  AdressBook
//
//  Created by 谭启宏 on 16/1/20.
//  Copyright © 2016年 谭启宏. All rights reserved.
//

#import "MainController.h"
#import "ViewController.h"
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
@interface MainController ()<CNContactPickerDelegate>

@property (nonatomic,strong)NSArray *dataArray;

@end

@implementation MainController

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@"iOS9打开电话薄",@"iOS9查看详情",@"iOS9添加联系人"];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];

}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
        {
            CNContactPickerViewController * con = [[CNContactPickerViewController alloc]init];
            con.delegate = self;
            [self presentViewController:con animated:YES completion:nil];
        }
            break;
        case 1:
        {
            //contact
//            CNContactViewController * con = [CNContactViewController viewControllerForContact:contact];
//            [self presentViewController:con animated:YES completion:nil];
        }
            break;
        case 2:
        {
            ViewController *vc = [[ViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma mark - CNContactPickerDelegate

//取消
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
    NSLog(@"取消");
}

//选中
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(nonnull CNContact *)contact {
    NSLog(@"点击了： %@",contact);
}

@end
